﻿Option Explicit On

Public Class frmFerramentasUsuarios

    Private Sub frmFerramentasUsuarios_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Enabled = False
        With cmbUsuarios
            .Add("ADMINISTRADOR", "admin")
            .Add("USUÁRIO", "user")
            .SelectedIndex = 1
        End With
        Me.Enabled = True
    End Sub

    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        If IsEmpty(txtSenha0.Text) Then
            Call MsgBox("Digite a nova senha!", MsgBoxStyle.Exclamation)
            txtSenha0.Select()
            Exit Sub
        End If
        If IsEmpty(txtSenha1.Text) Then
            Call MsgBox("Confirme a nova senha!", MsgBoxStyle.Exclamation)
            txtSenha1.Select()
            Exit Sub
        End If
        If txtSenha0.Text.Trim <> txtSenha1.Text.Trim Then
            Call MsgBox("Confirmação de senha não confere!", MsgBoxStyle.Exclamation)
            txtSenha1.Select()
            Exit Sub
        End If
        Call GravarDados()
    End Sub

    Private Sub GravarDados()
        Try
            Dim myPassword As New ClsCrypto
            Dim strFullPassword As String = txtSenha1.Text.Trim & "|" & strMySysPassword
            Dim strMD5Password As String = myPassword.GeraHashMD5(strFullPassword)

            mySQLite.Execute("UPDATE usuarios SET senha='" & strMD5Password & "' " & _
                             "WHERE usuario = '" & TrataNull(cmbUsuarios.Value, TypeCode.String) & "'")
            Call MsgBox("Senha trocada com sucesso!", MsgBoxStyle.Exclamation)
        Catch ex As Exception
            Call SendError(ex.Message, ex.StackTrace)
        End Try
    End Sub

End Class